<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>HyperCMF</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 注意：项目正式环境请勿引用该地址 -->
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <script src="/static/js/jquery.min.js?v=2.1.4"></script>
    <script src="/static/js/base.js"></script>
    <script src="/static/layui/layui.2.7.6.js"></script>
</head>
<body>
<div class="layui-fluid" style="margin-top: 10px">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">标题：</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" placeholder="请输入标题" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间：</label>
                        <div class="layui-input-block">
                            <input type="text" name="created_at" placeholder="请输入注册时间 区间" autocomplete="off"
                                   class="layui-input" id="test6" style="width:300px">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">状态：</label>
                        <div class="layui-input-block">
                            <select name="status" lay-verify="">
                                <option value=""></option>
                                <option value="1">已发布</option>
                                <option value="2">已下架</option>
                                <option value="3">已禁用</option>
                                <option value="0">未发布</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
            <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">

        <button class="layui-btn layui-btn-sm" lay-event="add">新增文章</button>
    </div>
</script>

<script type="text/html" id="cityTpl">
    <select id="City1" class="layui-border" lay-ignore>
        <option value="0">禁用</option>
        <option value="1">正常</option>
    </select>
</script>

<script type="text/html" id="bar">
    {{#  if(d.status ==1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="ban">禁用</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="down">下架</a>
    {{#  }else if(d.status ==3){ }}
    <a class="layui-btn layui-btn-default layui-btn-xs" lay-event="publish">解禁</a>
    {{#  }else if(d.status ==2){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="publish">上架</a>
    {{#  }else if(d.status ==0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="publish">发布</a>
    {{#  } }}
</script>

<script>
    layui.use(['table', 'dropdown','layer','laydate','form'], function(){
        var table = layui.table;
        var dropdown = layui.dropdown;
        var layer = layui.layer,form=layui.form;
        var laydate = layui.laydate;

        //日期范围
        laydate.render({
            elem: '#test6'
            ,type: 'datetime'
            ,range: '~'
        });
        // 创建渲染实例
        table.render({
            elem: '#dataTable'
            ,url:'/admin/article/index' // 此处为静态模拟数据，实际使用时需换成真实接口
            ,headers:{token:localStorage.getItem('hypercmfname')+'_'+localStorage.getItem('token')}
            ,toolbar: '#toolbar'
            ,defaultToolbar: ['filter', 'exports', 'print']
            ,height: 'full-200' // 最大高度减去其他容器已占有的高度差
            ,cellMinWidth: 80
            // ,totalRow: true // 开启合计行
            ,page: true
            ,response: {
                statusCode: 1 //重新规定成功的状态码为 200，table 组件默认为 0
            }
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                console.log(res.data.data)
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', fixed: 'left', width:80, title: 'ID', sort: true}
                ,{field:'title', width:120, title: '标题'}
                ,{field:'thumbnail', width:180, title: '封面图', templet:function(d){
                        return '<div class="layer-photos"><img layer-pid="'+d.id+'" layer-src="'+d.thumbnail+'" alt="'+d.title+'" src="'+d.thumbnail+'"></div>'
                    }}
                ,{field:'keywords', width:120, title: '关键词'}
                ,{field:'description', title:'简介' }
                ,{field:'status', width:80, title: '状态',templet:function(d){
                        if(d.status==0){
                            return '未发布'
                        }else if(d.status==1){
                            return '已发布'
                        }else if(d.status==2){
                            return '已下架'
                        }else if(d.status==3){
                            return '已禁用'
                        }}
                }
                ,{fixed: 'right', title:'操作', width: 200,minWidth: 125, toolbar: '#bar'}
            ]]
            ,done: function(){
                var id = this.id;
                layer.photos({
                    photos: '.layer-photos'
                    ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });
            }
            ,error: function(res, msg){
                console.log(res, msg)
            }
        });

        // 工具栏事件
        table.on('toolbar(dataTable)', function(obj){
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            var othis = lay(this);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(layui.util.escape(JSON.stringify(data)));
                    break;
                case 'add':
                    layer.open({
                        title: '新增文章',
                        type: 2,
                        shadeClose:true,
                        area: ['90%','90%'],
                        content: '/admin/article/add_article.html'
                    })
            };
        });

        //触发单元格工具事件
        table.on('tool(dataTable)', function(obj){ // 双击 toolDouble
            var data = obj.data;
            // console.log(obj)
            if(obj.event === 'edit'){
                layer.open({
                    title: '修改信息',
                    type: 2,
                    shadeClose:true,
                    area: ['90%','90%'],
                    content: '/admin/article/edit_article.html?a=1&id='+obj.data.id
                });
            }else if(obj.event === 'ban'){
                var a = Math.ceil(Math.random()*100)
                layer.prompt({title: '为了防止误操作，请输入：'+ a, formType: 2}, function(text, index){
                    if(text!=a){
                        layer.msg('验证失败');
                        setTimeout(function () {
                            layer.close(index);
                        },1000)
                    }else{
                        $.ajax({
                            url:'/admin/article/delArticle',
                            data:{id:obj.data.id,status:3},
                            async:false,
                            type:'post',
                            dataType:'json',
                            headers:{
                                'token':localStorage.getItem('hypercmfname')+'_'+localStorage.getItem('token'),
                                'accept':'*/*'
                            },
                            success:function(res){
                                if(res.code==1){
                                    layer.msg('操作成功',{
                                        icon: 1,
                                        time: 1000 //1秒关闭（如果不配置，默认是3秒）
                                    },function () {
                                        layer.closeAll()
                                        table.reload('dataTable')
                                    })

                                }else{
                                    layer.alert(res.msg,{icon:5})//1√，2×，3问号，4锁，5不开心，6开心
                                }
                            },error:function(res){
                                layer.alert('请求失败',{icon:2})
                            }
                        })
                    }
                });

            }else if(obj.event === 'publish'){
                var a = Math.ceil(Math.random()*100)
                layer.prompt({title: '为了防止误操作，请输入：'+ a, formType: 2}, function(text, index){
                    if(text!=a){
                        layer.msg('验证失败');
                        setTimeout(function () {
                            layer.close(index);
                        },1000)
                    }else{
                        $.ajax({
                            url:'/admin/article/editArticle',
                            data:{id:obj.data.id,status:1},
                            async:false,
                            type:'post',
                            dataType:'json',
                            headers:{
                                'token':localStorage.getItem('hypercmfname')+'_'+localStorage.getItem('token'),
                                'accept':'*/*'
                            },
                            success:function(res){
                                if(res.code==1){
                                    layer.msg('操作成功',{
                                        icon: 1,
                                        time: 1000 //1秒关闭（如果不配置，默认是3秒）
                                    },function () {
                                        layer.closeAll()
                                        table.reload('dataTable')
                                    })

                                }else{
                                    layer.alert(res.msg,{icon:5})//1√，2×，3问号，4锁，5不开心，6开心
                                }
                            },error:function(res){
                                layer.alert('请求失败',{icon:2})
                            }
                        })
                    }
                });

            }else if(obj.event === 'down'){
                var a = Math.ceil(Math.random()*100)
                layer.prompt({title: '为了防止误操作，请输入：'+ a, formType: 2}, function(text, index){
                    if(text!=a){
                        layer.msg('验证失败');
                        setTimeout(function () {
                            layer.close(index);
                        },1000)
                    }else{
                        $.ajax({
                            url:'/admin/article/editArticle',
                            data:{id:obj.data.id,status:2},
                            async:false,
                            type:'post',
                            dataType:'json',
                            headers:{
                                'token':localStorage.getItem('hypercmfname')+'_'+localStorage.getItem('token'),
                                'accept':'*/*'
                            },
                            success:function(res){
                                if(res.code==1){
                                    layer.msg('操作成功',{
                                        icon: 1,
                                        time: 1000 //1秒关闭（如果不配置，默认是3秒）
                                    },function () {
                                        layer.closeAll()
                                        table.reload('dataTable')
                                    })

                                }else{
                                    layer.alert(res.msg,{icon:5})//1√，2×，3问号，4锁，5不开心，6开心
                                }
                            },error:function(res){
                                layer.alert('请求失败',{icon:2})
                            }
                        })
                    }
                });

            }
        });
        form.on('submit(search)',function (data) {
            table.reload('dataTable',{
                where:data.field,
                page:{
                    curr:1
                }
            })

        })

    });

</script>

</body>
</html>